<?php

// 获取文件路径
$file_path = $_GET['file'];
error_log("get file starting readfile: " . $file_path);

// 检查目录名是否合法
$directory = strtok($file_path, '/');
$allowedPattern = '/^[a-zA-Z0-9_-]+$/';
if (!preg_match($allowedPattern, $directory)) {
    error_log($file_path);
    die('Invalid file name.');
}

// 构建完整文件路径
$base_path = "/var/www/html";
$file_path = $base_path . "/" . $file_path;

// 检查文件是否存在
$attempts = 0;
$max_attempts = 5;
$wait_time = 400; // 400毫秒

while (!file_exists($file_path) && $attempts < $max_attempts) {
    sleep($wait_time / 1000); // 等待400毫秒
    $attempts++;
}

if (file_exists($file_path)) {
    // 获取文件扩展名
    $extension = pathinfo($file_path, PATHINFO_EXTENSION);
    $file_name =  basename($file_path);
    $file_size = filesize($file_path);
    // 设置适当的 Content-Type 头
    switch ($extension) {
        case "css":
            header('Content-Type: text/css');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "apng":
            header('Content-Type: image/apng');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "avif":
            header('Content-Type: image/avif');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "gif":
            header('Content-Type: image/gif');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "jpeg":
        case "jpg":
        case "jfif":
        case "pjpeg":
        case "pjp":
            header('Content-Type: image/jpeg');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "png":
            header('Content-Type: image/png');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "svg":
        case "svgz":
            header('Content-Type: image/svg+xml');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "webp":
        case "awebp":
            header('Content-Type: image/webp');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "bmp":
            header('Content-Type: image/bmp');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "ico":
        case "cur":
            header('Content-Type: image/x-icon');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "tiff":
        case "tif":
            header('Content-Type: image/tiff');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "xbm":
            header('Content-Type: image/xbm');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "html":
            header('Content-Type: text/html');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "js":
            header('Content-Type: text/javascript');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "json":
            header('Content-Type: application/json');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "pdf":
            header('Content-Type: application/pdf');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "swf":
            header('Content-Type: application/x-shockwave-flash');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "txt":
            header('Content-Type: text/plain');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "wav":
            header('Content-Type: audio/x-wav');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "wma":
            header('Content-Type: audio/x-ms-wmv');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        case "xml":
            header('Content-Type: text/xml');
            header('Content-Disposition: inline; filename="' . $file_name . '"');
            header('Content-Length: ' . $file_size);
            break;
        default:
            $file_type = mime_content_type($file_path);
            if($file_type === 'application/zip' || $file_type == 'application/java-archive') {
                header("Content-Type: $file_type");
                header('Content-Disposition: attachment; filename="' . $file_name . '"');
                header('Content-Length: ' . $file_size);
            } else {
                header("Content-Type: $file_type");
                header('Content-Disposition: inline; filename="' . $file_name . '"');
                header('Content-Length: ' . $file_size); 
            }  
            break;
    }


    // 设置缓存头
    header('Cache-Control: public, max-age=864000'); // 缓存一年
    header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 864000) . ' GMT');

    // 直接读取并输出文件内容
    readfile($file_path);

    // 结束脚本
    exit;
} else {
    // 文件不存在
    header('HTTP/1.1 404 Not Found');
    echo 'File not found after ' . $attempts . ' attempts.';
    exit;
}

?>
